
package pe.com.yugi.DAO.Impl;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pe.com.yugi.DAO.CartaDAO;
import pe.com.yugi.DAO.PlanetaDAO;
import pe.com.yugi.Dominio.Carta;
import pe.com.yugi.Dominio.Planeta;
import pe.com.yugi.Util.HibernateUtil;


public class PlanetaDAOImpl implements PlanetaDAO, Serializable{

    @Override
    public boolean agregar(Planeta c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public boolean modificar(Planeta c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public List<Planeta> listarPlaneta() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Planeta order by nombre asc");
        List<Planeta> list = query.list();
        return list;
    }

    @Override
    public Planeta obtenerPlaneta(int idPlaneta) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Planeta where idPlaneta="+idPlaneta+" order by nombre asc");
        Planeta list = (Planeta) query.uniqueResult();
        return list;
    }
    
}
